package xyz.bali16.module.link.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import xyz.bali16.application.core.enums.ApiEnum;
import xyz.bali16.application.core.model.Result;
import xyz.bali16.module.link.entity.Link;
import xyz.bali16.module.link.model.LinkAddParam;
import xyz.bali16.module.link.model.LinkPageParam;
import xyz.bali16.module.link.model.LinkUpdateParam;
import xyz.bali16.module.link.service.LinkService;


import javax.validation.Valid;
import java.util.List;

import static xyz.bali16.application.core.model.Result.*;
import static xyz.bali16.module.link.utils.ResultUtils.errorLog;


/**
 * @author bali2 on 9/3/2023.
 * @version 1.0
 */
@RestController
@RequestMapping(BaseController.API+"link")
@Api(tags = "友情链接接口")
@AllArgsConstructor

public class LinkController extends BaseController{
    public static final Logger logger = LoggerFactory.getLogger(LinkController.class);
    @Autowired
    LinkService linkService;

    @GetMapping
    @ApiOperation("分页条件查询友情链接")
    //@PreAuthorize("@auth.permission('plugin:link:query')")
    public Result<IPage<Link>> pageQuery(@Valid LinkPageParam param) {
        IPage<Link> tagIPage = linkService.getPage(param);
        return success(ApiEnum.GET_SUCCESS, tagIPage);
    }

    @PostMapping
    //@PreAuthorize("@auth.permission('plugin:link:add')")
    @ApiOperation("添加友情链接")
    public Result<String> add(@RequestBody @Valid LinkAddParam param) {
        try {
            linkService.addLink(param);
            return response(ApiEnum.ADD_SUCCESS);
        } catch (Exception e) {
            errorLog(logger, e, "新增友情链接 '{}' 信息失败 {}", param.getName(), e.getMessage());
            return failure(ApiEnum.ADD_ERROR, "新增友情链接失败", e);
        }
    }

    @PutMapping
    //@PreAuthorize("@auth.permission('plugin:link:update')")
    @ApiOperation("修改友情链接")
    public Result<String> update(@RequestBody @Valid LinkUpdateParam param) {
        try {
            linkService.updateLink(param);
            return response(ApiEnum.UPDATE_SUCCESS);
        } catch (Exception e) {
            errorLog(logger, e, "更新友情链接 '{}' 信息失败 {}", param.getId(), e.getMessage());
            return failure(ApiEnum.UPDATE_ERROR, "更新友情链接失败", e);
        }
    }

    @DeleteMapping("/{linkId}")
    //@PreAuthorize("@auth.permission('plugin:link:delete')")
    @ApiOperation("删除友情链接")
    @ApiImplicitParam(name = "linkId", value = "友情链接id", paramType = "path", required = true)
    public Result<String> deleteLink(@PathVariable("linkId") String linkId) {
        try {
            linkService.delete(linkId);
            return response(ApiEnum.DELETE_SUCCESS);
        } catch (Exception e) {
            errorLog(logger, e, "删除友情链接 '{}' 失败. {}", linkId, e.getMessage());
            return failure(ApiEnum.DELETE_ERROR, e);
        }
    }

}
